import java.io.StringReader;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

class <tablename>UPDATE {
    public String getSQLStatement(String xmlData) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(false);
        dbf.setValidating(false);

        XPathFactory xpf = XPathFactory.newInstance();
        XPath x = xpf.newXPath();

        DocumentBuilder builder = dbf.newDocumentBuilder();
        Document document = builder.parse(new InputSource(new StringReader(xmlData)));
        document.normalizeDocument();

        NodeList data = document.getElementsByTagName("data");
        NodeList children = data.item(0).getChildNodes();

        NodeList tableSchemaNl = document.getElementsByTagName("schema-name");
        String schemaName = tableSchemaNl.item(0).getTextContent();

        NodeList tableNameNl = document.getElementsByTagName("table-name");
        String tableName = tableNameNl.item(0).getTextContent();

        NodeList primaryKey = document.getElementsByTagName("primary-key");
        NodeList pkCols = primaryKey.item(0).getChildNodes();

        StringBuffer sb = new StringBuffer("UPDATE ");
        sb.append(schemaName);
        sb.append(".");
        sb.append(tableName);
        sb.append(" SET ");

        for(int i=0; i<children.getLength(); i++){
            Element elem = (Element) children.item(i);
            String nodeName = elem.getNodeName();
            String nodeContent = elem.getTextContent();
            
            if (!"schema-name".equals(nodeName) && !"table-name".equals(nodeName)
                    && !"primary-key".equals(nodeName) && !"foreign-key".equals(nodeName)) {
                String autoIncr = elem.getAttribute("autoincr");
                if ("false".equals(autoIncr)) {
                    if (nodeContent.contains("'")) {
                        nodeContent=nodeContent.replace("'","\\'");
                    }
                    sb.append(nodeName);
                    sb.append(" = '");
                    sb.append(nodeContent);
                    if (i!=children.getLength()-1) {
                        sb.append("',");
                    } else {
                        sb.append("'");
                    }
                }
            }
        }
        sb.append(" WHERE ");

        for (int i=0; i<pkCols.getLength(); i++) {
            Element col = (Element) pkCols.item(i);
            String colName = col.getTextContent();

            NodeList value = (NodeList) x.evaluate("/data//"+colName, document,XPathConstants.NODESET);
            Element elem =  (Element)value.item(0);
            String colText = elem.getTextContent();

            sb.append(colName);
            sb.append(" = '");
            sb.append(colText);

            if (i!=children.getLength()-1) {
                sb.append("'");
            } else {
                sb.append("',");
            }
        }


        return sb.toString();
    }
}